package ruifeng.zcj.happymahjony.manager;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

public class ExitManager {

    public static long currentBackPressedTime = 0;

    private static final int BACK_PRESSED_INTERVAL = 2000;

    public static void onBackPressed(Context context) {
        if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
            currentBackPressedTime = System.currentTimeMillis();
            Toast.makeText(context, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            Exit(context);
        }
    }

    /**
     * 退出<br/>
     * 主线程中调用<br/>
     */
    public static void Exit(Context context) {
        clearStaticData();
        ((Activity) context).finish();
    }

    /**
     * 单例设计类必须清除静态数据<br/>
     */
    public static void clearStaticData() {

    }

    /**
     * 延迟退出<br/>
     * 主线程中调用<br/>
     */
    public static void ExitDelayed(final Context context) {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                Exit(context);
            }
        }, 3000);

    }

}
